#!/usr/bin/python
# Time execution of test files
from timeit import Timer
from sys import exit, argv, stderr
from fpformat import fix
import os

def parse_test(cpp_prep, test_name):
	stream = os.popen(cpp_prep+" "+test_name)
	success = False	
	must_fail = False
	scanning_ref = False
	reflines = []
	for line in stream.readlines():
		if line.find("}STOP_REF")!=-1: scanning_ref = False	
		line=line.replace("{TEST_FILE}",test_name)
		if scanning_ref: reflines.append(line)
		if line.find("START_REF{")!=-1: scanning_ref = True
		if line.find("{MUST_FAIL}")!=-1: must_fail = True

	stream.close()
	return (must_fail,reflines)

def run_test_cpp(cpp_runner,test_name,test_orig_name,must_fail,reflines):
	(stdin,stdout,stderr) = os.popen3(cpp_runner+" "+test_name+" -noexception_text")
	if must_fail: 
		stream=stderr
	else:
		stream=stdout

	count=1
	for line in stream.readlines():
		if len(reflines)==0: raise RuntimeError("Extra line(s) in "+test_orig_name)
		if line.strip() != reflines[0].strip(): raise RuntimeError("Unexpected output from "+
			test_orig_name+" in line "+str(count)+". \n"+
			"Reference val: "+reflines[0]+"Actual  value: "+line)
		del reflines[0]
		count=count+1

	if len(reflines)!=0: raise RuntimeError("Unmatched "+str(len(reflines))+
		" reference line(s) in "+test_orig_name)

cpp_runner = os.path.abspath(argv[1]).replace("\\","/")
cpp_prep = os.path.abspath(argv[2]).replace("\\","/")
test = os.path.abspath(argv[3]).replace("\\","/")
test_orig = argv[3]

try:
	(must_fail,reflines)=parse_test(cpp_prep,test)
	run_test_cpp(cpp_runner,test,test_orig,must_fail,reflines)
	print "'"+test_orig+"': **passed**"
	exit(0)
except RuntimeError,err:
	msg="'"+test_orig+"': **failed**\n"+str(err)
	stderr.write(msg)
	exit(1)
